<!DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><head>


<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>
	
        spring各种邮件发送 
        -
        lenolong的专栏
        - CSDN博客
</title>

    <script src="3970110.aspx_files/jquery-1.js" type="text/javascript"></script>

    <script src="3970110.aspx_files/jquery.js" type="text/javascript"></script>

    <script src="3970110.aspx_files/highlighter.js" type="text/javascript"></script>

    <script src="3970110.aspx_files/common.js" type="text/javascript"></script>

    <link href="3970110.aspx_files/Cogitation_1.css" type="text/css" rel="stylesheet" media="all"><link href="3970110.aspx_files/csdn_favbykimi.css" type="text/css" rel="stylesheet" media="all"><link rel="Shortcut Icon" href="http://profile.csdn.net/lenolong/picture/1.ico"><link href="http://feeds.feedsky.com/csdn.net/lenolong" title="RSS" type="application/rss+xml" rel="alternate"></head><body id="defaultuser">
    <div id="csdnblog_allwrap">
        <div id="csdnblog_midwrap">
            
<div id="csdnblog_header">
    <h1><a href="http://blog.csdn.net/lenolong">lenolong的专栏</a></h1><h2>♂商学子导航</h2>
    <ul id="personalnav">
		<li style=""><a id="a_login" href="http://passport.csdn.net/UserLogin.aspx?from=http%3A%2F%2Fblog.csdn.net%2Flenolong%2Farchive%2F2009%2F03%2F08%2F3970110.aspx">登录</a></li>
		<li style=""><a id="a_register" href="http://passport.csdn.net/CSDNUserRegister.aspx">注册</a></li>
		<li style="display: none;"><a id="a_welcome" href="http://hi.csdn.net/" target="_blank">欢迎</a></li>
		<li style="display: none;"><a id="a_exit" href="http://writeblog.csdn.net/Signout.aspx">退出</a></li>
		<li style="display: none;"><a id="a_myblog" href="http://blog.csdn.net/">我的博客</a></li>
		<li style="display: none;"><a id="a_configure" href="http://writeblog.csdn.net/configure.aspx" target="_blank">配置</a></li>
		<li style="display: none;"><a id="a_postedit" href="http://writeblog.csdn.net/PostEdit.aspx" target="_blank">写文章</a></li>
		<li style="display: none;"><a id="a_postlist" href="http://writeblog.csdn.net/PostList.aspx" target="_blank">文章管理</a></li>
		<li style="display: none;"><a id="a_bloghome" href="http://blog.csdn.net/" target="_blank">博客首页</a></li>
	</ul>
    <ul id="blogsearchsty">
		<li><input id="inputSearch" class="bolgsearch" type="text"></li>
		<li class="selectsty">
			<select name="Search:ddlSearchScope" id="Search_ddlSearchScope">
				<option selected="selected" value="all">全站</option>
				<option value="lenolong">当前博客</option>
			</select>
		</li>
		<li><input id="buttonSearch" value="搜索" class="bolggobtn" type="button"></li>
	</ul>
    <ul id="menu">
		<li><a href="http://hi.csdn.net/lenolong" target="_blank">空间</a></li>
		<li><a class="on" href="http://blog.csdn.net/lenolong">博客</a></li>
		<li><a href="http://hi.csdn.net/%21s/friend/list/lenolong" target="_blank">好友</a></li>
		<li><a href="http://hi.csdn.net/%21s/album/list/lenolong" target="_blank">相册</a></li>
		<li><a class="last" href="http://hi.csdn.net/%21s/wall/to/lenolong" target="_blank">留言</a></li>
	</ul>
</div>
<script type="text/javascript">
	var CurrentUserName = 'lenolong';
</script>
            
<div id="csdnblog_sidebar">
	<div class="gutter">
		<div class="aboutauthor">
			<dl>
				<dt style="text-align: left;">用户操作</dt>
				<dd class="middle">
					<a href="http://hi.csdn.net/%21s/wall/to/lenolong" target="_blank">[留言]</a>&nbsp;
					<a href="http://hi.csdn.net/%21s/msg/to/lenolong" target="_blank">[发消息]</a>&nbsp;
					<a href="http://hi.csdn.net/%21s/friend/add/lenolong" target="_blank">[加为好友]</a>&nbsp;
				</dd>
				<dt style="text-align: left;">商学子<span>ID：<a href="http://hi.csdn.net/lenolong" target="_blank">lenolong</a></span></dt><dd class="middle"><a href="http://hi.csdn.net/lenolong" target="_blank"><img class="avatar" src="3970110.aspx_files/1_lenolong.jpg" alt="商学子"></a></dd><dd style="text-align: left;">共<em>37415</em>次访问，排名<em>4105(-1)</em>，<a href="http://hi.csdn.net/%21s/friend/list/lenolong" target="_blank">好友</a><em>180</em>人，<a href="http://hi.csdn.net/%21s/follow/list/lenolong" target="_blank">关注者</a><em>182</em>人。</dd><dd style="text-align: left;">努力做一个真实的我，诚信的我，有用的我！要相信自己的实力，一定能战胜自我！！！</dd><dt style="text-align: left;">商学子的文章</dt><dd style="text-align: left;">原创 166 篇</dd><dd style="text-align: left;">翻译 0 篇</dd><dd style="text-align: left;">转载 91 篇</dd><dd style="text-align: left;">评论 30 篇</dd>
				<span id="SubscriptionList">
					<dt style="text-align: left;">订阅我的博客</dt>
					<dd style="text-align: left;">
						<a href="http://feeds.feedsky.com/csdn.net/lenolong">
							<img alt="XML聚合" src="3970110.aspx_files/gif.gif" border="0">
						</a>&nbsp;&nbsp;
						<a href="http://feeds.feedsky.com/csdn.net/lenolong" target="_blank">
							<img alt="FeedSky" src="3970110.aspx_files/feedsky.gif" border="0">
						</a>
					</dd>
					<dd style="text-align: left;">
						<a href="http://www.xianguo.com/subscribe.php?url=http://feeds.feedsky.com/csdn.net/lenolong" target="_blank">
							<img alt="订阅到鲜果" src="3970110.aspx_files/rss_xianguo.jpg" border="0">
						</a>
					</dd>
					<dd style="text-align: left;">
						<a href="http://fusion.google.com/add?feedurl=http://feeds.feedsky.com/csdn.net/lenolong" target="_blank">
							<img alt="订阅到Google" src="3970110.aspx_files/rss_google.gif" border="0">
						</a>
					</dd>
					<dd style="text-align: left;">
						<a href="http://www.zhuaxia.com/add_channel.php?url=http://feeds.feedsky.com/csdn.net/lenolong" target="_blank">
							<img alt="订阅到抓虾" src="3970110.aspx_files/rss_zhuaxia.gif" border="0">
						</a>
					</dd>
				</span>
				<dt style="text-align: left;">
					<span class="floatright" style="display: none;"><a class="a_edit" href="http://writeblog.csdn.net/configure.aspx" target="_blank">[编辑]</a></span>lenolong的公告
				</dt>
				<dd style="text-align: left;"></dd>
				<dt style="text-align: left;"><span class="floatright" style="display: none;"><a class="a_edit" href="http://writeblog.csdn.net/EditCategories.aspx?catID=1" target="_blank">[编辑]</a></span>文章分类</dt>
				<dd style="text-align: left;">
					<div class="publiclist_sidebar">
						<ul><li><a href="http://blog.csdn.net/lenolong/category/492469.aspx/rss"><img src="3970110.aspx_files/rss.gif" alt="(RSS)"></a><a href="http://blog.csdn.net/lenolong/category/492469.aspx">.net</a></li><li><a href="http://blog.csdn.net/lenolong/category/492470.aspx/rss"><img src="3970110.aspx_files/rss.gif" alt="(RSS)"></a><a href="http://blog.csdn.net/lenolong/category/492470.aspx">Ajax系列</a></li><li><a href="http://blog.csdn.net/lenolong/category/492643.aspx/rss"><img src="3970110.aspx_files/rss.gif" alt="(RSS)"></a><a href="http://blog.csdn.net/lenolong/category/492643.aspx">Dreamweaver</a></li><li><a href="http://blog.csdn.net/lenolong/category/526536.aspx/rss"><img src="3970110.aspx_files/rss.gif" alt="(RSS)"></a><a href="http://blog.csdn.net/lenolong/category/526536.aspx" title="整合flex与其他语言">Flex系列</a></li><li><a href="http://blog.csdn.net/lenolong/category/492471.aspx/rss"><img src="3970110.aspx_files/rss.gif" alt="(RSS)"></a><a href="http://blog.csdn.net/lenolong/category/492471.aspx">hibernate系列</a></li><li><a href="http://blog.csdn.net/lenolong/category/492472.aspx/rss"><img src="3970110.aspx_files/rss.gif" alt="(RSS)"></a><a href="http://blog.csdn.net/lenolong/category/492472.aspx">javaEE</a></li><li><a href="http://blog.csdn.net/lenolong/category/492473.aspx/rss"><img src="3970110.aspx_files/rss.gif" alt="(RSS)"></a><a href="http://blog.csdn.net/lenolong/category/492473.aspx">java世界</a></li><li><a href="http://blog.csdn.net/lenolong/category/492475.aspx/rss"><img src="3970110.aspx_files/rss.gif" alt="(RSS)"></a><a href="http://blog.csdn.net/lenolong/category/492475.aspx">JS系列</a></li><li><a href="http://blog.csdn.net/lenolong/category/492655.aspx/rss"><img src="3970110.aspx_files/rss.gif" alt="(RSS)"></a><a href="http://blog.csdn.net/lenolong/category/492655.aspx">Linux/CVS/SVN/VSFTPD...</a></li><li><a href="http://blog.csdn.net/lenolong/category/492477.aspx/rss"><img src="3970110.aspx_files/rss.gif" alt="(RSS)"></a><a href="http://blog.csdn.net/lenolong/category/492477.aspx">Spring系列</a></li><li><a href="http://blog.csdn.net/lenolong/category/492476.aspx/rss"><img src="3970110.aspx_files/rss.gif" alt="(RSS)"></a><a href="http://blog.csdn.net/lenolong/category/492476.aspx">Struts与Struts2系列</a></li><li><a href="http://blog.csdn.net/lenolong/category/492480.aspx/rss"><img src="3970110.aspx_files/rss.gif" alt="(RSS)"></a><a href="http://blog.csdn.net/lenolong/category/492480.aspx">xml系列</a></li><li><a href="http://blog.csdn.net/lenolong/category/494749.aspx/rss"><img src="3970110.aspx_files/rss.gif" alt="(RSS)"></a><a href="http://blog.csdn.net/lenolong/category/494749.aspx">工作流引擎jbpm</a></li><li><a href="http://blog.csdn.net/lenolong/category/493573.aspx/rss"><img src="3970110.aspx_files/rss.gif" alt="(RSS)"></a><a href="http://blog.csdn.net/lenolong/category/493573.aspx">面试经典</a></li><li><a href="http://blog.csdn.net/lenolong/category/492479.aspx/rss"><img src="3970110.aspx_files/rss.gif" alt="(RSS)"></a><a href="http://blog.csdn.net/lenolong/category/492479.aspx">设计模式</a></li><li><a href="http://blog.csdn.net/lenolong/category/492478.aspx/rss"><img src="3970110.aspx_files/rss.gif" alt="(RSS)"></a><a href="http://blog.csdn.net/lenolong/category/492478.aspx">数据库应用</a></li><li><a href="http://blog.csdn.net/lenolong/category/494745.aspx/rss"><img src="3970110.aspx_files/rss.gif" alt="(RSS)"></a><a href="http://blog.csdn.net/lenolong/category/494745.aspx">搜索引擎</a></li><li><a href="http://blog.csdn.net/lenolong/category/492481.aspx/rss"><img src="3970110.aspx_files/rss.gif" alt="(RSS)"></a><a href="http://blog.csdn.net/lenolong/category/492481.aspx">心情随笔</a></li><li><a href="http://blog.csdn.net/lenolong/category/499837.aspx/rss"><img src="3970110.aspx_files/rss.gif" alt="(RSS)"></a><a href="http://blog.csdn.net/lenolong/category/499837.aspx">中间件/集群</a></li></ul>
					</div>
				</dd>
				
				
				<dt style="text-align: left;">存档</dt>
				<dd style="text-align: left;">
					<div class="publiclist_sidebar">
						<ul><li><a href="http://blog.csdn.net/lenolong/archive/2009/11.aspx">2009年11月(1)</a></li><li><a href="http://blog.csdn.net/lenolong/archive/2009/10.aspx">2009年10月(2)</a></li><li><a href="http://blog.csdn.net/lenolong/archive/2009/03.aspx">2009年03月(35)</a></li><li><a href="http://blog.csdn.net/lenolong/archive/2009/02.aspx">2009年02月(7)</a></li><li><a href="http://blog.csdn.net/lenolong/archive/2009/01.aspx">2009年01月(6)</a></li><li><a href="http://blog.csdn.net/lenolong/archive/2008/12.aspx">2008年12月(206)</a></li></ul>
					</div>
				</dd>
			</dl>
		</div>
	</div>
</div>

            
<div id="csdnblog_content">
	<div class="gutter">
		<div class="default_contents">
			<div class="user_article">
				<script type="text/javascript" src="3970110.aspx_files/LoadFeedbackCount.js"></script>
				<h1 class="title_txt">
					<img src="3970110.aspx_files/authorship.gif" alt="原创" border="0" height="16" width="15">&nbsp;
					spring各种邮件发送 
					<cite class="fav_csdnstylebykimi">
						<a href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(saveit=window.open('http://wz.csdn.net/storeit.aspx?t='+escape(d.title)+'&amp;u='+escape(d.location.href)+'&amp;c='+escape(t),'saveit','scrollbars=no,width=590,height=300,left=75,top=20,status=no,resizable=yes'));saveit.focus();" class="fav_csdnstylebykimi" title="收藏到我的网摘中，并分享给我的朋友">收藏</a>
					</cite>
					
				</h1>
				<div class="blogstory">
					<script type="text/javascript">
						document.body.oncopy = function() {
							if (window.clipboardData) {
								setTimeout(function() {
									var text = clipboardData.getData("text"); 
									if (text && text.length>300) {
										text = text + "\r\n\n本文来自CSDN博客，转载请标明出处：" + location.href;
										clipboardData.setData("text", text); 
									}
								}, 100);
							}
						}
					</script>
					<script type="text/javascript">function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>
					<p>Spring邮件抽象层的主要包为<tt class="literal">org.springframework.mail</tt>。它包括了发送电子邮件的主要接口<tt class="interfacename">MailSender</tt>，和<span class="emphasis"><em>值对象</em></span><tt class="classname">SimpleMailMessage</tt>，它封装了简单邮件的属性如<span class="emphasis"><em>from</em></span>, <span class="emphasis"><em>to</em></span>,<span class="emphasis"><em>cc</em></span>, <span class="emphasis"><em>subject</em></span>,<span class="emphasis"><em>text</em></span>。 包里还包含一棵以<tt class="classname">MailException</tt>为根的checked Exception继承树，它们提供了对底层邮件系统异常的高级别抽象。 要获得关于邮件异常层次的更丰富的信息，请参考Javadocs。<br><br>为了使用<span class="emphasis"><em>JavaMail</em></span>中的一些特色, 比如MIME类型的信件, Spring提供了<tt class="interfacename">MailSender</tt>的一个子接口, 即<tt class="classname">org.springframework.mail.javamail.JavaMailSender</tt>。Spring还提供了一个回调接口<tt class="interfacename">org.springframework.mail.javamail.MimeMessagePreparator</tt>, 用于准备JavaMail的MIME信件。<br><br><strong>1.发送简单的文本邮件</strong></p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><span style="color: rgb(0, 0, 255);">package</span> <span style="color: rgb(0, 0, 0);">&nbsp;net.xftzr.mail;<br></span><span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);">&nbsp;java.util.Properties;<br><br></span><span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);">&nbsp;org.springframework.mail.SimpleMailMessage;&nbsp;<br></span><span style="color: rgb(0, 0, 255);">import</span> <span style="color: rgb(0, 0, 0);">&nbsp;org.springframework.mail.javamail.JavaMailSenderImpl;&nbsp;<br><br></span><span style="color: rgb(0, 128, 0);">/**</span> <span style="color: rgb(0, 128, 0);"><br>&nbsp;*&nbsp;本类测试简单邮件&nbsp;<br>&nbsp;*&nbsp;直接用邮件发送<br>&nbsp;*&nbsp;</span> <span style="color: rgb(128, 128, 128);">@author</span> <span style="color: rgb(0, 128, 0);">&nbsp;Administrator<br>&nbsp;*<br>&nbsp;</span> <span style="color: rgb(0, 128, 0);">*/</span> <span style="color: rgb(0, 0, 0);"><br></span><span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);">&nbsp;</span> <span style="color: rgb(0, 0, 255);">class</span> <span style="color: rgb(0, 0, 0);">&nbsp;SingleMailSend&nbsp;{&nbsp;<br>&nbsp;&nbsp;</span> <span style="color: rgb(0, 0, 255);">public</span> <span style="color: rgb(0, 0, 0);">&nbsp;</span> <span style="color: rgb(0, 0, 255);">static</span> <span style="color: rgb(0, 0, 0);">&nbsp;</span> <span style="color: rgb(0, 0, 255);">void</span> <span style="color: rgb(0, 0, 0);">&nbsp;main(String&nbsp;args[]){&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;JavaMailSenderImpl&nbsp;senderImpl&nbsp;</span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);">&nbsp;</span> <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(0, 0, 0);">&nbsp;JavaMailSenderImpl();&nbsp;<br>&nbsp;&nbsp;</span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">设定mail&nbsp;server&nbsp;</span> <span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.setHost(</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">smtp.163.com</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">);<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">建立邮件消息&nbsp;</span> <span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;SimpleMailMessage&nbsp;mailMessage&nbsp;</span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);">&nbsp;</span> <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(0, 0, 0);">&nbsp;SimpleMailMessage();&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">设置收件人，寄件人&nbsp;用数组发送多个邮件<br>&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">String[]&nbsp;array&nbsp;=&nbsp;new&nbsp;String[]&nbsp;&nbsp;&nbsp;&nbsp;{"sun111@163.com","sun222@sohu.com"};&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">mailMessage.setTo(array);&nbsp;</span> <span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;mailMessage.setTo(</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">toEmail@sina.com</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;mailMessage.setFrom(</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">userName@163.com</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;mailMessage.setSubject(</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">测试简单文本邮件发送！</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;mailMessage.setText(</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">测试我的简单邮件发送机制！！</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.setUsername(</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">userName</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;</span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">&nbsp;根据自己的情况,设置username</span> <span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.setPassword(</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">password</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;</span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">&nbsp;根据自己的情况,&nbsp;设置password</span> <span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;Properties&nbsp;prop&nbsp;</span> <span style="color: rgb(0, 0, 0);">=</span> <span style="color: rgb(0, 0, 0);">&nbsp;</span> <span style="color: rgb(0, 0, 255);">new</span> <span style="color: rgb(0, 0, 0);">&nbsp;Properties()&nbsp;;<br>&nbsp;prop.put(</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">mail.smtp.auth</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">,&nbsp;</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">true</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;</span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">&nbsp;将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确</span> <span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;prop.put(</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">mail.smtp.timeout</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">,&nbsp;</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">25000</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;<br>&nbsp;senderImpl.setJavaMailProperties(prop);&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span> <span style="color: rgb(0, 128, 0);">//</span> <span style="color: rgb(0, 128, 0);">发送邮件&nbsp;</span> <span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.send(mailMessage);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">邮件发送成功..</span> <span style="color: rgb(0, 0, 0);">"</span> <span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;<br>&nbsp;&nbsp;}&nbsp;<br></span></div>
<p><strong><br>2.发送简单的html邮件</strong> <br><span style="font-family: Courier New;">org.springframework.mail.javamail.MimeMessageHelper</span>是处理JavaMail邮件常用的顺手组件之一。它可以让你摆脱繁复的<tt class="literal">javax.mail.internet</tt>API类</p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);">&nbsp;net.xftzr.mail;<br><br><br></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;java.util.Properties;<br><br></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;javax.mail.internet.MimeMessage;&nbsp;<br></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;org.springframework.mail.javamail.JavaMailSenderImpl;&nbsp;<br></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;org.springframework.mail.javamail.MimeMessageHelper;&nbsp;<br></span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);">&nbsp;<br>&nbsp;*&nbsp;本类测试html邮件&nbsp;<br>&nbsp;*&nbsp;</span><span style="color: rgb(128, 128, 128);">@author</span><span style="color: rgb(0, 128, 0);">&nbsp;sunny&nbsp;<br>&nbsp;*&nbsp;<br>&nbsp;</span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);">&nbsp;<br></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;HTMLMailDemo&nbsp;{&nbsp;<br></span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);">&nbsp;<br>*&nbsp;</span><span style="color: rgb(128, 128, 128);">@param</span><span style="color: rgb(0, 128, 0);">&nbsp;args&nbsp;<br></span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);">&nbsp;<br></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;main(String[]&nbsp;args)&nbsp;</span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);">&nbsp;Exception{&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;JavaMailSenderImpl&nbsp;senderImpl&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;JavaMailSenderImpl();&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">设定mail&nbsp;server&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.setHost(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">smtp.163.com</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">建立邮件消息,发送简单邮件和html邮件的区别&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;MimeMessage&nbsp;mailMessage&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;senderImpl.createMimeMessage();&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;MimeMessageHelper&nbsp;messageHelper&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;MimeMessageHelper(mailMessage);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">设置收件人，寄件人&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;messageHelper.setTo(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">Mailto@sina.com</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;messageHelper.setFrom(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">username@163.com</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;messageHelper.setSubject(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">测试HTML邮件！</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">true&nbsp;表示启动HTML格式的邮件&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;messageHelper.setText(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;hello!!spring&nbsp;html&nbsp;Mail&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.setUsername(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">username</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;根据自己的情况,设置username</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.setPassword(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">password</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;根据自己的情况,&nbsp;设置password</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;Properties&nbsp;prop&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;Properties()&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;prop.put(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">mail.smtp.auth</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">true</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;prop.put(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">mail.smtp.timeout</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">25000</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.setJavaMailProperties(prop);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">发送邮件&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.send(mailMessage);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">邮件发送成功..</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>}&nbsp;<br>}&nbsp;</span></div>
<p><span style="color: rgb(0, 0, 0);"><strong><br>3.发送嵌套图片的邮件</strong></span><br><br>Email允许添加附件，也允许在multipart信件中内嵌资源。内嵌资源可能是你在信件中希望使用的图像，或者样式表，但是又不想把它们作为附件。</p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);">&nbsp;net.xftzr.mail;<br><br></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;java.io.File;&nbsp;<br></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;java.util.Properties;<br><br></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;javax.mail.internet.MimeMessage;&nbsp;<br></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;org.springframework.core.io.FileSystemResource;&nbsp;<br></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;org.springframework.mail.javamail.JavaMailSenderImpl;&nbsp;<br></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;org.springframework.mail.javamail.MimeMessageHelper;&nbsp;<br></span><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);">&nbsp;<br>&nbsp;*&nbsp;本类测试邮件中嵌套图片&nbsp;<br>&nbsp;*&nbsp;</span><span style="color: rgb(128, 128, 128);">@author</span><span style="color: rgb(0, 128, 0);">&nbsp;sunny&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;*&nbsp;<br>&nbsp;</span><span style="color: rgb(0, 128, 0);">*/</span><span style="color: rgb(0, 0, 0);">&nbsp;<br></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;AttachedImageMail&nbsp;{&nbsp;<br></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;main(String[]&nbsp;args)&nbsp;</span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);">&nbsp;Exception{&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;JavaMailSenderImpl&nbsp;senderImpl&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;JavaMailSenderImpl();&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">设定mail&nbsp;server&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.setHost(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">smtp.163.com</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br><br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">建立邮件消息,发送简单邮件和html邮件的区别&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;MimeMessage&nbsp;mailMessage&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;senderImpl.createMimeMessage();&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">注意这里的boolean,等于真的时候才能嵌套图片，在构建MimeMessageHelper时候，所给定的值是true表示启用，&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">multipart模式&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;MimeMessageHelper&nbsp;messageHelper&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;MimeMessageHelper(mailMessage,</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">设置收件人，寄件人&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;messageHelper.setTo(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">toMail@sina.com</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;messageHelper.setFrom(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">username@163.com</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;messageHelper.setSubject(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">测试邮件中嵌套图片!！</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">true&nbsp;表示启动HTML格式的邮件&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;messageHelper.setText(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;hello!!spring&nbsp;image&nbsp;html&nbsp;mail&lt;/h1&gt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 0);">+</span><span style="color: rgb(0, 0, 0);">&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&lt;img&nbsp;src=\</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">cid:aaa\</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">/&gt;&lt;/body&gt;&lt;/html&gt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;FileSystemResource&nbsp;img&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;FileSystemResource(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;File(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">g:/123.jpg</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">));&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;messageHelper.addInline(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">aaa</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,img);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.setUsername(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">username</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;根据自己的情况,设置username</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.setPassword(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">password</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;根据自己的情况,&nbsp;设置password</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;Properties&nbsp;prop&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;Properties()&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;prop.put(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">mail.smtp.auth</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">true</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;prop.put(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">mail.smtp.timeout</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">25000</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.setJavaMailProperties(prop);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">发送邮件&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.send(mailMessage);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">邮件发送成功..</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>}&nbsp;<br>}&nbsp;</span></div>
<p><br><strong>4.发送包含附件的邮件</strong></p>
<div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; font-size: 13px; width: 98%; background-color: rgb(238, 238, 238);"><span style="color: rgb(0, 0, 255);"><span style="color: rgb(0, 0, 255);">package</span><span style="color: rgb(0, 0, 0);">&nbsp;net.xftzr.mail;<br><br><br><br></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;java.io.File;&nbsp;<br></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;java.util.Properties;<br><br></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;javax.mail.internet.MimeMessage;&nbsp;<br></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;org.springframework.core.io.FileSystemResource;&nbsp;<br></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;org.springframework.mail.javamail.JavaMailSenderImpl;&nbsp;<br></span><span style="color: rgb(0, 0, 255);">import</span><span style="color: rgb(0, 0, 0);">&nbsp;org.springframework.mail&nbsp;.javamail.MimeMessageHelper;&nbsp;<br></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);">&nbsp;AttachedFileMail&nbsp;</span><span id="Codehighlighter1_330_1665_Open_Text"><span style="color: rgb(0, 0, 0);">{&nbsp;<br></span><span id="Codehighlighter1_333_376_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);"><span style="color: rgb(0, 0, 0);">/**&nbsp;*/</span></span><span id="Codehighlighter1_333_376_Open_Text"><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);">&nbsp;<br>*&nbsp;本类测试的是关于邮件中带有附件的例子&nbsp;<br>*&nbsp;</span><span style="color: rgb(128, 128, 128);">@param</span><span style="color: rgb(0, 128, 0);">&nbsp;args&nbsp;<br></span><span style="color: rgb(0, 128, 0);">*/</span></span><span style="color: rgb(0, 0, 0);">&nbsp;<br></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;main(String[]&nbsp;args)&nbsp;</span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);">&nbsp;Exception</span><span id="Codehighlighter1_434_1662_Open_Text"><span style="color: rgb(0, 0, 0);">{&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;JavaMailSenderImpl&nbsp;senderImpl&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;JavaMailSenderImpl();&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">设定mail&nbsp;server&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.setHost(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">smtp.163.com</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">建立邮件消息,发送简单邮件和html邮件的区别&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;MimeMessage&nbsp;mailMessage&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;senderImpl.createMimeMessage();&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">注意这里的boolean,等于真的时候才能嵌套图片，在构建MimeMessageHelper时候，所给定的值是true表示启用，&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">multipart模式&nbsp;为true时发送附件&nbsp;可以设置html格式</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;MimeMessageHelper&nbsp;messageHelper&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;MimeMessageHelper(mailMessage,</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">utf-8</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">设置收件人，寄件人&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;messageHelper.setTo(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">toMail@sina.com</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;messageHelper.setFrom(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">username@163.com</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;messageHelper.setSubject(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">测试邮件中上传附件!！</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">true&nbsp;表示启动HTML格式的邮件&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;messageHelper.setText(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;你好：附件中有学习资料！&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;FileSystemResource&nbsp;file&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;FileSystemResource(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;File(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">g:/test.rar</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">));&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">这里的方法调用和插入图片是不同的。&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;messageHelper.addAttachment(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">test.rar</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,file);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.setUsername(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">username</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;根据自己的情况,设置username</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.setPassword(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">password</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;根据自己的情况,&nbsp;设置password</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;Properties&nbsp;prop&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;Properties()&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;prop.put(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">mail.smtp.auth</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">true</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;prop.put(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">mail.smtp.timeout</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">25000</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.setJavaMailProperties(prop);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">发送邮件&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.send(mailMessage);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">邮件发送成功..</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>}</span></span><span style="color: rgb(0, 0, 0);">&nbsp;<br>}</span></span></span><span id="Codehighlighter1_330_1665_Open_Text"><span id="Codehighlighter1_333_376_Closed_Text" style="border: 1px solid rgb(128, 128, 128); display: none; background-color: rgb(255, 255, 255);">**&nbsp;*/</span><span id="Codehighlighter1_333_376_Open_Text"><span style="color: rgb(0, 128, 0);">/**</span><span style="color: rgb(0, 128, 0);">&nbsp;<br>*&nbsp;本类测试的是关于邮件中带有附件的例子&nbsp;<br>*&nbsp;</span><span style="color: rgb(128, 128, 128);">@param</span><span style="color: rgb(0, 128, 0);">&nbsp;args&nbsp;<br></span><span style="color: rgb(0, 128, 0);">*/</span></span><span style="color: rgb(0, 0, 0);">&nbsp;<br></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">static</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">void</span><span style="color: rgb(0, 0, 0);">&nbsp;main(String[]&nbsp;args)&nbsp;</span><span style="color: rgb(0, 0, 255);">throws</span><span style="color: rgb(0, 0, 0);">&nbsp;Exception</span><span id="Codehighlighter1_434_1662_Open_Text"><span style="color: rgb(0, 0, 0);">{&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;JavaMailSenderImpl&nbsp;senderImpl&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;JavaMailSenderImpl();&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">设定mail&nbsp;server&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.setHost(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">smtp.163.com</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">建立邮件消息,发送简单邮件和html邮件的区别&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;MimeMessage&nbsp;mailMessage&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;senderImpl.createMimeMessage();&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">注意这里的boolean,等于真的时候才能嵌套图片，在构建MimeMessageHelper时候，所给定的值是true表示启用，&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">multipart模式&nbsp;为true时发送附件&nbsp;可以设置html格式</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;MimeMessageHelper&nbsp;messageHelper&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;MimeMessageHelper(mailMessage,</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">utf-8</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">设置收件人，寄件人&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;messageHelper.setTo(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">toMail@sina.com</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;messageHelper.setFrom(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">username@163.com</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;messageHelper.setSubject(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">测试邮件中上传附件!！</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">true&nbsp;表示启动HTML格式的邮件&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;messageHelper.setText(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">&lt;html&gt;&lt;head&gt;&lt;/head&gt;&lt;body&gt;&lt;h1&gt;你好：附件中有学习资料！&lt;/h1&gt;&lt;/body&gt;&lt;/html&gt;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,</span><span style="color: rgb(0, 0, 255);">true</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;FileSystemResource&nbsp;file&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;FileSystemResource(</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;File(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">g:/test.rar</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">));&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">这里的方法调用和插入图片是不同的。&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;messageHelper.addAttachment(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">test.rar</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,file);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.setUsername(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">username</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;根据自己的情况,设置username</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.setPassword(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">password</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;根据自己的情况,&nbsp;设置password</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;Properties&nbsp;prop&nbsp;</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">&nbsp;</span><span style="color: rgb(0, 0, 255);">new</span><span style="color: rgb(0, 0, 0);">&nbsp;Properties()&nbsp;;<br>&nbsp;&nbsp;&nbsp;&nbsp;prop.put(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">mail.smtp.auth</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">true</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">&nbsp;将这个参数设为true，让服务器进行认证,认证用户名和密码是否正确</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;prop.put(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">mail.smtp.timeout</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">,&nbsp;</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">25000</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">)&nbsp;;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.setJavaMailProperties(prop);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;</span><span style="color: rgb(0, 128, 0);">//</span><span style="color: rgb(0, 128, 0);">发送邮件&nbsp;</span><span style="color: rgb(0, 128, 0);"><br></span><span style="color: rgb(0, 0, 0);">&nbsp;&nbsp;&nbsp;&nbsp;senderImpl.send(mailMessage);&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;<br>&nbsp;&nbsp;&nbsp;&nbsp;System.out.println(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">邮件发送成功..</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);&nbsp;<br>}</span></span><span style="color: rgb(0, 0, 0);">&nbsp;<br>}</span></span></div>
					<p class="right articalinfo">
						发表于 @ 2009年03月08日　21:45:00&nbsp;|
						<a id="a_comment" href="#FeedBack" title="评论">
							评论(
							<span id="FeedbackCount_3970110">0</span>
							<script type="text/javascript">
								AddFeedbackCountStack("3970110")
							</script>)
						</a>|
						<span style="display: none;"><a class="a_edit" href="http://writeblog.csdn.net/PostEdit.aspx?entryId=3970110" title="编辑">编辑</a>|</span>
						<a href="mailto:webmaster@csdn.net?subject=Article%20Report%21%21%21&amp;body=Author:lenolong%0D%0AURL:http://blog.csdn.net/ArticleContent.aspx?UserName=lenolong&amp;Entryid=3970110">举报</a>|
						<cite class="fav_csdnstylebykimi">
							<a href="javascript:d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(saveit=window.open('http://wz.csdn.net/storeit.aspx?t='+escape(d.title)+'&amp;u='+escape(d.location.href)+'&amp;c='+escape(t),'saveit','scrollbars=no,width=590,height=300,left=75,top=20,status=no,resizable=yes'));saveit.focus();" class="fav_csdnstylebykimi" title="收藏到我的网摘中，并分享给我的朋友">收藏</a>
						</cite>
					</p>
					<span id="Post.ascx_ViewPost_PreviousAndNextEntriesDown"><h3 class="pagego"><a href="http://blog.csdn.net/lenolong/archive/2009/03/08/3967471.aspx">旧一篇:webwork中解决action跳转到action丢失参数的问题</a>&nbsp;|&nbsp;<a href="http://blog.csdn.net/lenolong/archive/2009/03/11/3981317.aspx">新一篇:安装 jBPM 的 Eclipse 开发插件</a></h3></span>
				</div>
			</div>
			<a name="FeedBack"></a>
			<div class="commentslist">
				<script type="text/javascript">
					var CurrentEntryId = '3970110';
				</script>
				<script type="text/javascript" src="3970110.aspx_files/feedback.js"></script>
				<div id="commentslist"></div>
			</div>
			<div class="spacecommment">
			<fieldset><legend>给lenolong的留言<dfn>只有注册用户才能发表评论！<a href="http://passport.csdn.net/member/UserLogin.aspx?from=http://blog.csdn.net/lenolong/archive/2009/03/08/3970110.aspx">登录</a><a href="http://passport.csdn.net/CSDNUserRegister.aspx">注册</a></dfn></legend><em><img src="3970110.aspx_files/blog_commentnotice.gif" alt="" title=""></em><ul><li>姓&nbsp;&nbsp;&nbsp;名：<input size="25" type="text"></li><li>校验码：<input size="25" type="text"></li></ul><textarea cols="50" rows="8"></textarea><input value="提交留言" class="submitbtn" type="button"></fieldset></div>
			<script type="text/javascript">
			LoadFeedbackCount();
			document.write("<img src='http://counter.csdn.net/pv.aspx?id=24' border=0 width=0 height=0>");
			</script><img src="3970110.aspx_files/pv.txt" border="0" height="0" width="0">
		</div>
	</div>
</div>
            
<div id="pubfooter">
<dl>
	<dt></dt>
	<dd>Copyright © lenolong</dd>
	<dd>Powered by CSDN Blog</dd>
</dl>
</div>
        </div>
    </div>
    <img src="3970110.aspx_files/count.htm" height="0" width="0">

    <script type="text/javascript" src="3970110.aspx_files/counter.js"></script><script type="text/javascript" charset="UTF-8" src="3970110.aspx_files/msg.js"></script><img src="3970110.aspx_files/visitlog.htm" alt="" border="0" height="1" width="1"><iframe id="myframe" name="myframe" border="0" src="3970110.aspx_files/dd333.htm" scrolling="no" frameborder="no" height="0" width="0"></iframe>

    <script type="text/javascript" src="3970110.aspx_files/ga.js"></script>

    <script type="text/javascript">
        try {
            var pageTracker = _gat._getTracker("UA-2688088-9");
            pageTracker._trackPageview();
        } catch (err) { }
    </script>

</body></html>